package student;

import java.util.Arrays;
import java.util.Comparator;

class Student{
//class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

//    @Override
//    public int compareTo(Student o) {
//        //根据年龄比较
//        if(this.age>o.age){
//            return 1;
//        }else if(this.age==o.age){
//            return 0;
//        }else{
//            return -1;
//        }
//        //根据分数比较
////        if(this.score>o.score){
////            return 1;
////        }else if(this.score==o.score){
////            return 0;
////        }else{
////            return -1;
////        }
//    }
//    //这种接口重写的写法对类的侵入性太强,可以选择下面的比较器实现接口
}
//年龄比较器
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1,Student o2){
        return o1.age- o2.age;
    }
}
//成绩比较器
class ScoreComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score- o2.score);
    }
}

public class StudentSort {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("张三",18,99.99);
        students[1] = new Student("李四",20,89.99);
        students[2] = new Student("王五",19,100.00);
        AgeComparator ageComparator = new AgeComparator();
        ScoreComparator scoreComparator= new ScoreComparator();
        Arrays.sort(students,ageComparator);
        System.out.println("排序后:"+Arrays.toString(students));
        Arrays.sort(students,scoreComparator);
        System.out.println("排序后:"+Arrays.toString(students));
    }
}
